package org.yaac.server.egql;

import static com.google.common.base.Strings.isNullOrEmpty;

import java.io.Serializable;

/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
public class FromEntity implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String entityName;
	
	private String alias;

	public FromEntity(String entityName, String alias) {
		super();
		this.entityName = entityName;		
		this.alias = isNullOrEmpty(alias) ? entityName : alias;
	}

	public FromEntity(String entityName) {
		this(entityName, null);
	}
	
	public String getEntityName() {
		return entityName;
	}

	public String getAlias() {
		return alias;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((alias == null) ? 0 : alias.hashCode());
		result = prime * result + ((entityName == null) ? 0 : entityName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		FromEntity other = (FromEntity) obj;
		if (alias == null) {
			if (other.alias != null)
				return false;
		} else if (!alias.equals(other.alias))
			return false;
		if (entityName == null) {
			if (other.entityName != null)
				return false;
		} else if (!entityName.equals(other.entityName))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "FromEntity [entityName=" + entityName + ", alias=" + alias + "]";
	}
}